package com.zh.common.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import com.zh.common.entity.ElecSmsEntity;
import com.zh.common.entity.JSONResult;
import com.zh.common.constans.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 短信发送工具类
 * @Author: Li.HuiNeng
 * @Create in: 2020-03-04 11:22
 * @Version: 1.0.1
 */
@Component
public class SendSMSUtil {


    private static final String AccessKeyId = "LTAIHKZGaVRiOqeR";
    private static final String AccessSecret = "9IRpWqjSi7hmSLMAEEcZqjCdVPQp0L";

    /**
     * @Description:  短信验证码
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2020/3/4
     */
    public static JSONResult<Object> sendSms(ElecSmsEntity smsEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("RegionId", "cn-hangzhou");
        // 接收短信的手机号码。
        request.putQueryParameter("PhoneNumbers", smsEntity.getPhoneNumber());
        // 短信签名名称。
        request.putQueryParameter("SignName", AesEncodeUtil.decrypt(smsEntity.getSignName()));
        // 短信模板ID。
        request.putQueryParameter("TemplateCode", smsEntity.getCode());
        // 短信模板变量对应的实际值，JSON格式。
        Random random = new Random(new Date().getTime());
        // 随机生成6位验证码
        String randomNumber = "";
        for (int i = 1; i < 7; i++) {
            randomNumber += random.nextInt(10);
        }
        // 替换模板变量
        request.putQueryParameter("TemplateParam", "{\"code\": " + randomNumber +"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            HashMap map = JSON.parseObject(response.getData(), HashMap.class);
            if (map.get("Code").equals("OK")) {
                map.put("Captcha", randomNumber);
                map.put("PhoneNumbers",smsEntity.getPhoneNumber());
                jsonResult.setData(map);
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage(map.get("Message").toString());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return jsonResult;
    }


    /**
     * @Description: 批量发送短信通知
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2020/3/4
     */
/*        //用参数替换模板中的${}变量
        Matcher m = Pattern.compile("\\$\\{\\w+\\}").matcher(smsEntity.getTemplateParam());
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String param = m.group(); // 模板变量
            m.appendReplacement(sb, randomNumber);
        }
        m.appendTail(sb);
        System.out.println(sb);*/

    /* 测试 */
    public static void main(String[] args) throws Exception {
        String s = "" + ("0" + 1);
        System.out.println(s);
      /*  ElecSmsEntity elecSmsEntity = new ElecSmsEntity();
        elecSmsEntity.setCode("SMS_184815747");
        elecSmsEntity.setPhoneNumber("15919044428");
        elecSmsEntity.setTemplateParam("付惠吧温馨提示！您正在申请手机注册，验证码为：${code}，5分钟内有效！");
        elecSmsEntity.setSignName("11Wmek9KqnY28HW2dEnWVg==");
        sendSms(elecSmsEntity);*/
    }
}
